package com.banshuibao.bsb_android.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.Base64ForImg;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.view.MyViewPager;
import com.yalantis.ucrop.util.BitmapLoadUtils;

import uk.co.senab.photoview.PhotoView;

public class BitmapShowActivity extends BaseActivity {

    private MyViewPager mPager;
    private String[] images;
    private int index, length;
    private TextView images_nums;
    private String webImage;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_show);
        images = getIntent().getStringArrayExtra("images");
        index = getIntent().getIntExtra("index", 0);
        webImage = getIntent().getStringExtra("webImage");
        if (!TextUtils.isEmpty(webImage)) {
            String image = context.getSharedPreferences("webImage", Context.MODE_PRIVATE).getString(webImage, "");
            bitmap = Base64ForImg.stringtoBitmap(image.split(",")[1]);
        }
        if (images != null)
            length = images.length;
        else
            length = 1;
        initView();
        setVp();
    }

    public void initView() {
        mPager = findViewById(R.id.vp);
        images_nums = findViewById(R.id.images_nums);
        if (index == 0)
            setImagesIndex(1);
    }

    private void setImagesIndex(int num) {
        if (images != null && images.length > 1) {
            images_nums.setText(num + "/" + length);
        }
    }

    public void setVp() {
        mPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setImagesIndex(position + 1);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view = new PhotoView(BitmapShowActivity.this);
                // view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                if (bitmap == null)
                    ImageUtil.loadImage(BitmapShowActivity.this, images[position], view);
                else
                    view.setImageBitmap(bitmap);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        mPager.setCurrentItem(index);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        if (images != null) {
            images = null;
        }
    }
}
